import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import UnoCSS from 'unocss/vite';
import Components from 'unplugin-vue-components/vite';
import AutoImport from 'unplugin-auto-import/vite';
import { TinyVueSingleResolver } from '@opentiny/unplugin-tiny-vue';
import { codeInspectorPlugin } from 'code-inspector-plugin';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { viteMockServe } from 'vite-plugin-mock'
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    viteMockServe({
      mockPath: './src/mocks/viteMockServe',
      logger: true,
      // enable: process.env.NODE_ENV === 'development', // 替代原来的 localEnabled
      enable: true, // 替代原来的 localEnabled
      // ignore: /\.(vue|components|js|css|ts)/  // 明确忽略Vue文件
      ignore: /^(\.\/|\.\.\/)?(node_modules|@opentiny)\//,

    }),
    vueJsx(),
    UnoCSS({
      configFile: 'uno.config.ts',
    }),
    Components({
      resolvers: [TinyVueSingleResolver],
    }),
    AutoImport({
      resolvers: [TinyVueSingleResolver],
    }),
    {
      name: 'msw-middleware',
      configureServer(server) {
        server.middlewares.use((req, res, next) => {
          // @ts-ignore
          if (req.url?.includes('mockServiceWorker')) {
            res.setHeader('Content-Type', 'application/javascript');
          }
          next();
        });
      },
    },
    codeInspectorPlugin({
      bundler: 'vite',
      editor: 'trae',
    }),
  ],
  base: '/', // 确保 base 路径正确
  build: {
    outDir: 'dist',
    assetsDir: 'assets', // 静态资源目录
  },
  publicDir: 'public', // 确认 public 目录指向正确
  resolve: {
    alias: {
      '@': '/src',
      '~': '/src/components',
    },
  },
  define: {
    'process.env': { TINY_MODE: 'pc' },
  },
  server: {
    host: '0.0.0.0',
    // port: 911,
    // proxy: {
    //   '/api': {
    //     // target: 'http://localhost:911',
    //     target: 'http://real-server.com', // 会覆盖 Mock
    //     changeOrigin: true,
    //     rewrite: (path: string) => {
    //       return path.replace(/^\/api/, '');
    //     },
    //   },
    // },
    fs: {
      strict: true,
      allow: ['..'] // 允许访问项目根目录
    }
  },
});
